﻿using System.Linq;
using System.Web.ModelBinding;

namespace Morus.Presentation
{
    // ModelBinderProviders.Providers.Insert(0, new BooleanModelBinderProvider());
    public sealed class BooleanModelBinderProvider : ModelBinderProvider
    {
        private static readonly ModelBinderProvider[] _providers = new ModelBinderProvider[]
        {
            new SimpleModelBinderProvider(typeof(bool), new BooleanModelBinder()),
            new SimpleModelBinderProvider(typeof(bool?), new BooleanModelBinder())
        };

        public override IModelBinder GetBinder(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)
        {
            return (from provider in _providers
                    let binder = provider.GetBinder(modelBindingExecutionContext, bindingContext)
                    where binder != null
                    select binder).FirstOrDefault<IModelBinder>();
        }
    }
}
